package com.zkk.dao.impl;

import com.zkk.dao.TransferAccountDao;
import com.zkk.pojo.TransferAccount;
import com.zkk.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.math.BigDecimal;
import java.util.List;

public class TransferAccountDaoImpl implements TransferAccountDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());
    @Override
    public int add(TransferAccount transferAccount) {
        int update = jdbcTemplate.update("insert into transferAccount values (?,?,?,?)", transferAccount.getName(), transferAccount.getCard_no(), transferAccount.getBank(), null);
        return update;
    }



    @Override
    public int transferMoney(String card_no, BigDecimal money) {
        int update = jdbcTemplate.update("update transferAccount set property = ? where card_no = ?", money, card_no);
        return update;
    }

    @Override
    public TransferAccount queryByCard_no(String card_no) {
        List<TransferAccount> query = jdbcTemplate.query("select * from transferAccount where card_no = ?", new BeanPropertyRowMapper<>(TransferAccount.class), card_no);
        if(query!=null & query.size()==1){
            return query.get(0);
        }
        return null;
    }
}
